/*
 * ComerciosAgregarFrame.java
 *
 * Created on 16 de abril de 2008, 12:06 PM
 */
package org.remolques.placas;

import org.mybeans.Sesion;
import org.mybeans.ContribuyentesBeans;
import org.mybeans.ComerciosBeans;
 


import org.mybeans.RemolquesBeans;
import org.remolques.busquedas.RemolquesBusquedaSinPlacaFrame;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.sirmupadbapi.beans.Remolques;
import org.sirmupadbapi.beans.Vehicles;
import org.sirmupadbapi.controller.RemolquesController;
import org.sirmupadbapi.controller.VehiclesController;

/**
 *
 * @author  avbravo
 */
public class RemolquesAsignarPlacaFrame extends javax.swing.JFrame {

    Sesion sesion;
    ContribuyentesBeans contribuyentesBeans;
    ComerciosBeans comerciosBeans;
   RemolquesBeans remolquesBeans;
    boolean focoRemolque = false;
    int IdRemolque;

    /** Creates new form ComerciosAgregarFrame */
    public RemolquesAsignarPlacaFrame() {
        initComponents();
        sesion = new Sesion();
        comerciosBeans = new ComerciosBeans();
        contribuyentesBeans = new ContribuyentesBeans();
        remolquesBeans = new RemolquesBeans();
        /*
         * Fondo
         */

        
          
         

         
         

        /*
         * fin del fondo
         */

        java.util.Calendar ca = java.util.Calendar.getInstance();
        java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());


        jXDatePickerFechaLiquidacion.setDate(mydate);
        jXDatePickerFechaLiquidacion.setFormats(new SimpleDateFormat("dd/MM/yyyy"));


//        java.util.Calendar ca = java.util.Calendar.getInstance();
  //      java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());
  //      jFormattedTextFieldFechaInscripcion.setValue(mydate);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldIdRemolque = new javax.swing.JTextField();
        jTextFieldMarca = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldColor = new javax.swing.JTextField();
        jTextFieldModelo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jButtonBuscar = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel16 = new javax.swing.JLabel();
        jRadioButtonContribuyente = new javax.swing.JRadioButton();
        jRadioButtonComercio = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldId = new javax.swing.JTextField();
        jTextFieldDueno = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextFieldPlaca = new javax.swing.JTextField();
        jButtonGuardar = new javax.swing.JButton();
        jButtonLimpiar = new javax.swing.JButton();
        jButtonRegresar = new javax.swing.JButton();
        jLabel19 = new javax.swing.JLabel();
        jFormattedTextFieldFechaInscripcion = new javax.swing.JFormattedTextField();
        jLabel20 = new javax.swing.JLabel();
        jComboBoxMesPago = new javax.swing.JComboBox();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jTextFieldLiquidacion = new javax.swing.JTextField();
        jXDatePickerFechaLiquidacion = new org.jdesktop.swingx.JXDatePicker();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ASIGNAR PLACAS A REMOLQUES\t\t");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Numero");

        jLabel2.setText("Marca");

        jTextFieldIdRemolque.setColumns(35);
        jTextFieldIdRemolque.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldIdRemolqueActionPerformed(evt);
            }
        });

        jTextFieldMarca.setEditable(false);
        jTextFieldMarca.setEnabled(false);

        jLabel6.setText("Color");

        jTextFieldColor.setEditable(false);
        jTextFieldColor.setEnabled(false);

        jTextFieldModelo.setEditable(false);
        jTextFieldModelo.setEnabled(false);

        jLabel3.setText("Modelo");

        jButtonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/search.png"))); // NOI18N
        jButtonBuscar.setToolTipText("Buscar");
        jButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarActionPerformed(evt);
            }
        });

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel16.setText("Dueño es");

        jRadioButtonContribuyente.setSelected(true);
        jRadioButtonContribuyente.setText("Contribuyente");
        jRadioButtonContribuyente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRadioButtonContribuyenteMouseClicked(evt);
            }
        });

        jRadioButtonComercio.setText("Comercio");
        jRadioButtonComercio.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRadioButtonComercioMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabel16)
                .addGap(18, 18, 18)
                .addComponent(jRadioButtonContribuyente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButtonComercio)
                .addContainerGap(164, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel16)
            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jRadioButtonComercio)
                .addComponent(jRadioButtonContribuyente))
        );

        jLabel4.setText("Identificador");

        jTextFieldId.setEditable(false);
        jTextFieldId.setEnabled(false);

        jTextFieldDueno.setEditable(false);
        jTextFieldDueno.setEnabled(false);

        jLabel7.setText("Dueño");

        jLabel8.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel8.setText("Numero Placa");

        jButtonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/save.png"))); // NOI18N
        jButtonGuardar.setToolTipText("Guardar");
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jButtonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/escoba.png"))); // NOI18N
        jButtonLimpiar.setToolTipText("Limpiar");
        jButtonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimpiarActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jLabel19.setText("Fecha Inscripcion");

        jFormattedTextFieldFechaInscripcion.setEditable(false);
        jFormattedTextFieldFechaInscripcion.setEnabled(false);
        jFormattedTextFieldFechaInscripcion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFormattedTextFieldFechaInscripcionActionPerformed(evt);
            }
        });

        jLabel20.setText("Mes Pago");

        jComboBoxMesPago.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" }));
        jComboBoxMesPago.setEnabled(false);

        jLabel22.setText("Fecha Liquidacion");

        jLabel23.setText("No. Liquidacion");

        jTextFieldLiquidacion.setEditable(false);
        jTextFieldLiquidacion.setEnabled(false);

        jXDatePickerFechaLiquidacion.setToolTipText("dia/mes/año");
        jXDatePickerFechaLiquidacion.setEnabled(false);
        jXDatePickerFechaLiquidacion.setLinkDay(new java.util.Date(1261630800000L));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(25, 25, 25)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldModelo)
                            .addComponent(jTextFieldMarca, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldIdRemolque, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4)
                                .addComponent(jLabel7))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextFieldDueno, javax.swing.GroupLayout.PREFERRED_SIZE, 324, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel22)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jXDatePickerFechaLiquidacion, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(125, 125, 125))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel19)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jFormattedTextFieldFechaInscripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel23)
                                .addComponent(jLabel20))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jComboBoxMesPago, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTextFieldLiquidacion, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(38, 38, 38)
                        .addComponent(jTextFieldColor, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(70, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldIdRemolque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldMarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTextFieldDueno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel19)
                    .addComponent(jFormattedTextFieldFechaInscripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXDatePickerFechaLiquidacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel22))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel23)
                    .addComponent(jTextFieldLiquidacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel20)
                    .addComponent(jComboBoxMesPago, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jTextFieldIdRemolqueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldIdRemolqueActionPerformed

    CargarRemolques();
}//GEN-LAST:event_jTextFieldIdRemolqueActionPerformed

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jButtonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimpiarActionPerformed

    Limpiar();
}//GEN-LAST:event_jButtonLimpiarActionPerformed

    @SuppressWarnings({"static-access", "static-access"})
private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarActionPerformed

    try {
        String SNumero = this.jTextFieldIdRemolque.getText();

        if (SNumero == null || SNumero.equals("")) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar una remolque", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldIdRemolque.requestFocus();
            return;
        }
        IdRemolque = Integer.parseInt(SNumero);
        if (IdRemolque <= 0) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar un remolque", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldIdRemolque.requestFocus();
            return;
        }

        String Placa = this.jTextFieldPlaca.getText();

        if (Placa == null || Placa.equals("")) {
            JOptionPane.showMessageDialog(this, "Debe indicar el numero de placa", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldPlaca.requestFocus();
            return;
        }
 Remolques remolques = new Remolques();
        RemolquesController remolquesController = new RemolquesController();
        remolques.setPLACA(Placa);
          remolques.setIDMUNICIPIO(Sesion.getIdmunicipio());
          if(remolquesController.findByPLACA(remolques)){
               JOptionPane.showMessageDialog(this, "Ya existe un remolque con esa placa.Verifique los datos....", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
               return;
          }
          
        String sFechaInscripcion = jFormattedTextFieldFechaInscripcion.getText();
        if (sFechaInscripcion == null || sFechaInscripcion.equals("")) {
            JOptionPane.showMessageDialog(this, "seleccione la fecha de inscripcion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jFormattedTextFieldFechaInscripcion.requestFocus();
        }

      //  java.util.Date FechaDate = jXDatePickerFechaLiquidacion.getDate();
       //     java.sql.Date FechaInscripcion = new java.sql.Date(FechaDate.getTime());
       //     if (FechaInscripcion == null) {
       //         JOptionPane.showMessageDialog(this, "Ingrese la fecha de inscripcion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
       //         jXDatePickerFechaLiquidacion.requestFocus();
      //          return;
      //      }


        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        String sqlquery = "select * from remolques where idmunicipio = ? and idremolque =? and activo='si'";
        PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
        psquery.setString(1, sesion.getIdmunicipio());
        psquery.setInt(2, IdRemolque);
        ResultSet rs = psquery.executeQuery();
        rs.next();
        int x = rs.getRow();

        if (x == 0) {
            JOptionPane.showMessageDialog(this, "No existe un remolque con ese codigo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            sesion.con.close();
            return;
        }
        String placa = rs.getString("placa");
        if (placa == null || placa.equals("")) {
            boolean ok;
        } else {
            JOptionPane.showMessageDialog(this, "Este remolque ya tiene una placa asignada. ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

  String sql = "update  remolques set placa =?   where idmunicipio = ? and idremolque = ? and activo='si'";
        PreparedStatement prest = sesion.con.prepareStatement(sql);
        prest.setString(1, Placa);
 
        prest.setString(2, sesion.getIdmunicipio()); //placa
        prest.setInt(3, IdRemolque);
        int count = prest.executeUpdate();


        sesion.con.close();
        sesion.setIdRemolque(IdRemolque);
        sesion.setPlaca(Placa);
        JOptionPane.showMessageDialog(this, "Numero de placa asigando exitosamente...", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jButtonGuardar.setEnabled(false);



    } catch (Exception ex) {
        System.out.println("Error" + ex);
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonGuardarActionPerformed

private void jButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarActionPerformed

     RemolquesBusquedaSinPlacaFrame remolquesBusquedaSinPlacaFrame = new RemolquesBusquedaSinPlacaFrame();
    remolquesBusquedaSinPlacaFrame.setLocationRelativeTo(null);
    remolquesBusquedaSinPlacaFrame.setVisible(true);
    focoRemolque = true;
}//GEN-LAST:event_jButtonBuscarActionPerformed

private void jRadioButtonContribuyenteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButtonContribuyenteMouseClicked
}//GEN-LAST:event_jRadioButtonContribuyenteMouseClicked

private void jRadioButtonComercioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButtonComercioMouseClicked
}//GEN-LAST:event_jRadioButtonComercioMouseClicked

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    // TODO add your handling code here:
    try {

        if (focoRemolque) {

            Actualizar();

        }
        focoRemolque = false;
    } catch (Exception ex) {
        sesion.MostrarMensajeError("Error ", "formWindowGainedFocus()\nError " + ex.getMessage().toString());
    }
}//GEN-LAST:event_formWindowGainedFocus

private void jFormattedTextFieldFechaInscripcionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFormattedTextFieldFechaInscripcionActionPerformed
}//GEN-LAST:event_jFormattedTextFieldFechaInscripcionActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RemolquesAsignarPlacaFrame().setVisible(true);
            }
        });
    }

    private void Actualizar() {
        try {
            String placa = remolquesBeans.getPlaca();
            if (placa == null || placa.equals("")) {
                boolean ok;
            } else {
                JOptionPane.showMessageDialog(this, "Este remolque ya tiene una placa asignada. ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            jTextFieldIdRemolque.setText(String.valueOf(remolquesBeans.getIdRemolque()));
            jTextFieldMarca.setText(remolquesBeans.getMarca());
            jTextFieldModelo.setText(remolquesBeans.getModelo());
                   jTextFieldColor.setText(remolquesBeans.getColor());
            jTextFieldId.setText(remolquesBeans.getId());
            jTextFieldDueno.setText(remolquesBeans.getDueno());
           jFormattedTextFieldFechaInscripcion.setText(String.valueOf(remolquesBeans.getFechaInscripcion()));


            //       java.util.Calendar ca = java.util.Calendar.getInstance();
       // java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());


        jXDatePickerFechaLiquidacion.setDate(remolquesBeans.getFechaLiquidacion());
        jXDatePickerFechaLiquidacion.setFormats(new SimpleDateFormat("dd/MM/yyyy"));
           jTextFieldLiquidacion.setText(remolquesBeans.getLiquidacion());
            String TipoId = remolquesBeans.getTipoId();
            TipoId = TipoId.toLowerCase();
            if (TipoId.equals("contribuyentes")) {
                jRadioButtonContribuyente.setSelected(true);
                jRadioButtonComercio.setSelected(false);
            } else {
                if (TipoId.equals("comercios")) {
                    jRadioButtonContribuyente.setSelected(false);
                    jRadioButtonComercio.setSelected(true);
                }
            }
            jButtonGuardar.setEnabled(true);
            repaint();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void Limpiar() {
        this.jTextFieldIdRemolque.setText("");
        this.jTextFieldMarca.setText("");
        this.jTextFieldModelo.setText("");
        this.jTextFieldColor.setText("");
        this.jTextFieldId.setText("");
        this.jTextFieldDueno.setText("");
this.jTextFieldPlaca.setText("");
        jButtonGuardar.setEnabled(false);
    }

    private void CargarRemolques() {


        try {
         String   sIdRemolque = this.jTextFieldIdRemolque.getText();
            if (sIdRemolque  == null || sIdRemolque .equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese el codigo del remolque", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
         IdRemolque = Integer.parseInt(sIdRemolque);
            remolquesBeans.setIdRemolque(IdRemolque);

            if (remolquesBeans.Consultar() == true) {



                jTextFieldId.setText(remolquesBeans.getId());
                remolquesBeans.setTipoId(remolquesBeans.getTipoId().toLowerCase());
                if (remolquesBeans.getTipoId().equals("contribuyentes")) {
                    ContribuyentesCargar();
                    jRadioButtonContribuyente.setSelected(true);
                    jRadioButtonComercio.setSelected(false);
                } else {
                    if (remolquesBeans.getTipoId().equals("comercios")) {
                        ComerciosCargar();
                        jRadioButtonContribuyente.setSelected(false);
                        jRadioButtonComercio.setSelected(true);
                    }
                }

                Actualizar();

            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void ContribuyentesCargar() {
        try {
            String cedula = this.jTextFieldId.getText();
            if (cedula == null || cedula.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese la cedula del contribuyente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            String sqlquery = "select * from contribuyentes where idmunicipio = ? and cedula = ?";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, sesion.getIdmunicipio());
            psquery.setString(2, cedula);
            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();
            if (x == 0) {
                JOptionPane.showMessageDialog(this, "No existen registros de contribuyentes con esa cedula", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            String activo = rs.getString("activo");
            if (activo.equals("no")) {
                JOptionPane.showMessageDialog(this, "Este registro esta inactivo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            contribuyentesBeans.setCedula(cedula);
            contribuyentesBeans.setContribuyente(rs.getString("contribuyente"));
            contribuyentesBeans.setIdSeguroSocial(rs.getString("idsegurosocial"));
            contribuyentesBeans.setOcupacion(rs.getString("ocupacion"));
            contribuyentesBeans.setNacionalidad(rs.getString("nacionalidad"));
            contribuyentesBeans.setIdMunicipio(rs.getString("idmunicipio"));
            contribuyentesBeans.setIdCorregimiento(rs.getString("idcorregimiento"));
            contribuyentesBeans.setDomicilio(rs.getString("domicilio"));
            contribuyentesBeans.setCasa(rs.getString("casa"));
            contribuyentesBeans.setTelefono(rs.getString("telefono"));
            contribuyentesBeans.setApartadoPostal(rs.getString("apartadopostal"));
            contribuyentesBeans.setEmail(rs.getString("email"));
            contribuyentesBeans.setObservacion(rs.getString("observacion"));
            contribuyentesBeans.setIdMunicipioVive(rs.getString("idmunicipiovive"));
            contribuyentesBeans.setIdCorregimientoVive(rs.getString("idcorregimientovive"));
            contribuyentesBeans.setActivo(rs.getString("activo"));
            contribuyentesBeans.setFoto(rs.getString("foto"));



            if (contribuyentesBeans.getContribuyente() == null || contribuyentesBeans.getContribuyente().equals("")) {
                JOptionPane.showMessageDialog(this, "El contribuyente esta en blanco", "Mensaje", JOptionPane.WARNING_MESSAGE);
                return;
            }

            jTextFieldDueno.setText(contribuyentesBeans.getContribuyente());
            remolquesBeans.setDueno(contribuyentesBeans.getContribuyente());

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void ComerciosCargar() {
        try {
            String IdComercio = this.jTextFieldId.getText();
            if (IdComercio == null || IdComercio.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese el codigo del comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            String sqlquery = "select comercios.*, contribuyentes.contribuyente from comercios, contribuyentes where comercios.idmunicipio = ? and comercios.idcomercio = ? and comercios.cedula = contribuyentes.cedula";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, sesion.getIdmunicipio());
            psquery.setString(2, IdComercio);
            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();
            if (x == 0) {
                JOptionPane.showMessageDialog(this, "No existen registros de comercios con ese codigo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            String comercio = rs.getString("comercio");
            String cedula = rs.getString("cedula");
            String contribuyente = rs.getString("contribuyente");
            String Ruc = rs.getString("ruc");
            String Descripcion = rs.getString("descripcion");
            String Direccion = rs.getString("direccion");
            String IdCorregimiento = rs.getString("idcorregimiento");
            String Telefono = rs.getString("telefono");
            String Hipotecado = rs.getString("hipotecado");
            String Bajado = rs.getString("bajado");
            String Traspaso = rs.getString("traspaso");
            String Estatal = rs.getString("estatal");
            String Secuestrado = rs.getString("secuestrado");
            String Activo = rs.getString("activo");

            jTextFieldDueno.setText(comercio);

            if (comercio == null || comercio.equals("")) {
                JOptionPane.showMessageDialog(this, "El comercio esta en blanco", "Mensaje", JOptionPane.WARNING_MESSAGE);
                return;
            }

            comerciosBeans.setIdComercio(IdComercio);
            comerciosBeans.setComercio(comercio);
            comerciosBeans.setCedula(cedula);
            comerciosBeans.setContribuyente(contribuyente);
            comerciosBeans.setRuc(Ruc);
            comerciosBeans.setDescripcion(Descripcion);
            comerciosBeans.setDireccion(Direccion);
            comerciosBeans.setIdCorregimiento(IdCorregimiento);
            comerciosBeans.setTelefono(Telefono);
            comerciosBeans.setHipotecado(Hipotecado);
            comerciosBeans.setBajado(Bajado);
            comerciosBeans.setTraspaso(Traspaso);
            comerciosBeans.setEstatal(Estatal);
            comerciosBeans.setSecuestrado(Secuestrado);
            comerciosBeans.setActivo(Activo);
            remolquesBeans.setDueno(comerciosBeans.getComercio());
            repaint();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JComboBox jComboBoxMesPago;
    private javax.swing.JFormattedTextField jFormattedTextFieldFechaInscripcion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JRadioButton jRadioButtonComercio;
    private javax.swing.JRadioButton jRadioButtonContribuyente;
    private javax.swing.JTextField jTextFieldColor;
    private javax.swing.JTextField jTextFieldDueno;
    private javax.swing.JTextField jTextFieldId;
    private javax.swing.JTextField jTextFieldIdRemolque;
    private javax.swing.JTextField jTextFieldLiquidacion;
    private javax.swing.JTextField jTextFieldMarca;
    private javax.swing.JTextField jTextFieldModelo;
    private javax.swing.JTextField jTextFieldPlaca;
    private org.jdesktop.swingx.JXDatePicker jXDatePickerFechaLiquidacion;
    // End of variables declaration//GEN-END:variables
}
